package com.my.admin.controller;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.my.admin.entity.User;
import com.my.admin.entity.UserRole;
import com.my.admin.service.UserRoleService;
import com.my.admin.service.UserService;
import com.my.admin.util.Rest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin
public class UserController {

    @Autowired
    private UserService service;

    @Autowired
    private UserRoleService urService;

    @PostMapping("user/get")
    public Rest users(@RequestParam(defaultValue = "1") int pageNo, @RequestParam(defaultValue = "10") int pageSize, @RequestBody User user) {
        return Rest.ok().setData("page", service.page(new Page<>(pageNo, pageSize), new QueryWrapper<User>().select("id", "name", "username", "phone", "email", "status", "create_time", "update_time").
                like(StrUtil.isNotBlank(user.getName()), "name", user.getName()).like(StrUtil.isNotBlank(user.getUsername()), "username", user.getUsername()).
                like(StrUtil.isNotBlank(user.getPhone()), "phone", user.getPhone()).like(StrUtil.isNotBlank(user.getEmail()), "email", user.getEmail())));
    }

    @PostMapping("users")
    public Rest add(@RequestBody User user) {
        if(user.getPassword() == null) {
            user.setPassword("123456");
        }
        user.setPassword(SecureUtil.md5(user.getUsername() + user.getPassword()));
        if(service.save(user)) {
            return Rest.ok("添加成功！");
        }else {
            return Rest.err("添加失败!");
        }
    }

    @PutMapping("users")
    public Rest edit(@RequestBody User user) {
        if(StrUtil.isNotBlank(user.getPassword())) {
            user.setPassword(SecureUtil.md5(user.getUsername() + user.getPassword()));
        }
        if(service.updateById(user)) {
            return Rest.ok("修改成功！");
        }else {
            return Rest.err("修改失败！");
        }
    }

    @DeleteMapping("users")
    public Rest del(@RequestParam long id) {
        if(service.removeById(id)) {
            urService.remove(new QueryWrapper<UserRole>().eq("user_id", id));
            return Rest.ok("删除成功！");
        }else {
            return Rest.err("删除失败！");
        }
    }

    @GetMapping("user/repeat")
    public Rest repeat(@RequestParam String username, @RequestParam(defaultValue = "-1") Long id) {
        QueryWrapper<User> qw = new QueryWrapper<User>().eq("username", username);
        if(id != -1) {
            qw.ne("id", id);
        }
        return Rest.ok().setData("flag", service.getOne(qw) != null);
    }

    @PostMapping("user/status")
    public Rest status(@RequestBody User user) {
        if(service.updateById(user)) {
            return Rest.ok("状态修改成功！");
        }else {
            return Rest.err("状态修改失败！");
        }
    }

    @PostMapping("user/userRole")
    public Rest userRole(@RequestParam Long userId, @RequestParam(required = false) String[] roles) {
        urService.remove(new QueryWrapper<UserRole>().eq("user_id", userId));
        if(roles == null || urService.batchInsert(userId, Convert.toLongArray(roles))) {
            return Rest.ok("用户角色分配成功！");
        }
        return Rest.err("用户角色分配失败！");
    }

}
